elWeight <- matrix(0,sum(amat>0),3)
ind <- 1
for(i in 1:nrow(amat)){
	for(j in 1:ncol(amat)){
		if(amat[i,j] >0){
			elWeight[ind,1] <- rownames(amat)[i]
			elWeight[ind,2] <- colnames(amat)[j]
			elWeight[ind,3] <- amat[i,j]
			ind <- ind +1
		}
	}
print(i)
}

library(igraph)

g = graph.edgelist(elWeight[,c(1,2)])
E(g)$weight = as.numeric(elWeight[,3])
system.time(pos <- layout.fruchterman.reingold(g))


# Vertex attribute identification
vertNames <- V(g)$name
cands <- vertNames[which(substr(vertNames,1,1)=="N")]
repub <- candDat$Party[match(cands,candDat$CID)]=="R"
incumb <- substr(candDat$FirstLastP[match(cands,candDat$CID)],1,1)=="I" 
win <- substr(candDat$FirstLastP[match(cands,candDat$CID)],3,3)=="W" 

### first everyone ###
candCol <- rep(rgb(255,255,255,0.0001,maxColorValue=255),length(vertNames),length(cands))
candCol[repub & incumb] <- "red"
candCol[repub & !incumb] <- "darkorange"
candCol[!repub & incumb] <- "blue"
candCol[!repub & !incumb] <- "cornflowerblue"

vcol <- rep(rgb(255,255,255,0.0001,maxColorValue=255),length(vertNames))
vcol[match(cands,vertNames)] <- candCol

filet <- paste("/home/bruce/Dropbox/Camp/Tex/","network",t,".pdf",sep="")
pdf(filet,width=6,heigh=6,pointsize=10)
par(bg="black")
plot(g,layout=pos,vertex.color=vcol,vertex.size=.3,vertex.label=NA,edge.arrow.size=0,vertex.frame.color=NA,edge.color=rgb(255,255,255,7.5,maxColorValue=255),ylim=c(-.2,.2),xlim=c(-.1,.1))
dev.off()

### Now just winners ###
candCol <- rep(rgb(255,255,255,0.0001,maxColorValue=255),length(vertNames),length(cands))
candCol[repub & incumb & win] <- "red"
candCol[repub & !incumb & win] <- "darkorange"
candCol[!repub & incumb & win] <- "blue"
candCol[!repub & !incumb & win] <- "cornflowerblue"

vcol <- rep(rgb(255,255,255,0.0001,maxColorValue=255),length(vertNames))
vcol[match(cands,vertNames)] <- candCol

filet <- paste("/home/bruce/Dropbox/Camp/Tex/","networkW",t,".pdf",sep="")
pdf(filet,width=6,heigh=6,pointsize=10)
par(bg="black")
plot(g,layout=pos,vertex.color=vcol,vertex.size=.3,vertex.label=NA,edge.arrow.size=0,vertex.frame.color=NA,edge.color=rgb(255,255,255,7.5,maxColorValue=255),ylim=c(-.2,.2),xlim=c(-.1,.1))
dev.off()




